﻿using GetSlaughterItem.API.Application.Commands.Week02.LiveAnimalsa;
using MediatR;
using SlaughterItem.Common.Domains.Check_Ins;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Domains.Saleser;
using SlaughterItem.Common.Domains.UPCTable;
using SlaughterItem.Common.Domains.Warehouse;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week03.leos
{
    public class SaleSuretenHandler : IRequestHandler<SaleSuretenCommend, Result>
    {
        private readonly IBaseRepositoryGet<Sale> Sale;
        private readonly IBaseRepositoryGet<CheckIn> CheckIn;
        private readonly IBaseRepositoryGet<UPCData> UPCData;
        private readonly IBaseRepositoryGet<Product> Product;
        private readonly IBaseRepositoryGet<DepotType> DepotType;

        public SaleSuretenHandler(IBaseRepositoryGet<Sale> Sale, IBaseRepositoryGet<CheckIn> CheckIn
            , IBaseRepositoryGet<UPCData> UPCData, IBaseRepositoryGet<Product> Product, IBaseRepositoryGet<DepotType> DepotType)
        {
            this.Sale = Sale;
            this.CheckIn = CheckIn;
            this.UPCData = UPCData;
            this.Product = Product;
            this.DepotType = DepotType;
        }

        public async Task<Result> Handle(SaleSuretenCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };

            try
            {
                var list = (from a in Sale.GetAll()
                           join b in CheckIn.GetAll() on a.CheckInId equals b.CheckInId
                           join c in UPCData.GetAll() on a.MarkId equals c.GoodsMinuteId
                           join d in Product.GetAll() on a.ProductId equals d.ProductId
                           join e in DepotType.GetAll() on a.DepotId equals e.DepotTypeId
                           where a.IsDelete == false
                           select new
                           {
                               a.SaleId,
                               a.SaleNum,
                               a.SaleGrossWeight,
                               a.SaleTares,
                               a.SaleWeight,
                               a.SaleRemark,
                               a.SaleTime,
                               b.CheckInId,
                               b.CheckInNo,
                               c.GoodsMinuteId,
                               c.UPCDataName,
                               d.ProductId,
                               d.ProductName,
                               e.DepotTypeId,
                               e.DepotTypeName,
                           }).ToList();

                var totalcount = list.Count();
                var pagecount = (int)Math.Ceiling((totalcount * 1.0) / request.pageSize);
                list = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();

                var lief = new
                {
                    totalcount,
                    pagecount,
                    list,
                };

                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = lief;
                    result.Msg = "查询成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
